define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form, JsTree) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'data/supplier.json',
                    add_url: 'supplier-edit.html',
                    edit_url: 'supplier-edit.html',
                    del_url: 'data/del.json',
                    multi_url: '',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        //该列为复选框字段,如果后台的返回state值将会默认选中
                        { field: 'state', checkbox: true, },
						{ field: 'id', title: '序号', operate: false },
                        {
                            field: 'is_out', title: '淘汰', searchList: ['是', '否'], formatter: function (value, row, index) {
                                var rValue = "";
                                if (value == "是") {
                                    rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                                } else if (value == "否") {
                                    rValue = '<a class="btn btn-xs btn-danger">' + value + '</a>';
                                } else if (value == "外企") {
                                    rValue = '<a class="btn btn-xs btn-primary">' + value + '</a>';
                                } else {
                                    rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                                }
                                return rValue;
                            }
                        },
                        {
                            field: 'is_ok', title: '合格', searchList: ['是', '否'], formatter: function (value, row, index) {
                                var rValue = "";
                                if (value == "是") {
                                    rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                                } else if (value == "否") {
                                    rValue = '<a class="btn btn-xs btn-danger">' + value + '</a>';
                                } else if (value == "外企") {
                                    rValue = '<a class="btn btn-xs btn-primary">' + value + '</a>';
                                } else {
                                    rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                                }
                                return rValue;
                            }
                        },
                        { field: 'area', title: '所属地区' },
                        { field: 'typeone', title: '大类', searchList: ['物资类', '工程服务类'] },
                        { field: 'typetwo', title: '中类', visible: false, searchList: ['维修材料', '设施设备维保', '清洁用品/设备', '工程安装/维修'] },
                        { field: 'typethree', title: '小类', visible: false },
                        { field: 'idcode', title: '编号', visible: false },
                        { field: 'companyname', title: '供应商全称'},
						{ field: 'name', title: '联系人', visible: false },
						{ field: 'mobile', title: '联系电话', visible: false },
						{ field: 'idcard', visible: false, title: '统一社会信用代码', operate: false },
						{ field: 'address', visible: false, title: '地址', operate: false },
						{ field: 'is_taxpayer', visible: false, title: '是否一般纳税人',operate: false },
						{ field: 'is_zzszyfp', visible: false, title: '是否可开增值税专用发票', operate: false },
						{ field: 'registered_capital', visible: false, title: '注册资本', operate: false },
						{
						    field: 'away', title: '合作方式', searchList: ['常用', '协议合同', '协议年标', '一般'], formatter: function (value, row, index) {
						        var rValue = "";
						        if (value == "常用") {
						            rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
						        } else if (value == "协议合同") {
						            rValue = '<a class="btn btn-xs btn-danger">' + value + '</a>';
						        } else if (value == "协议年标") {
						            rValue = '<a class="btn btn-xs btn-primary">' + value + '</a>';
						        } else {
						            rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
						        }
						        return rValue;
						    }
						},
                        { field: 'is_join', visible: false, title: '地产监察是否参与考察', operate: false },
                        { field: 'startdate', visible: false, title: '考察合格开始时间', operate:false },
                        { field: 'enddate', visible: false, title: '有效期', operate: 'BETWEEN', type: 'datetime', addclass: 'datetimepicker', data: 'data-date-format="YYYY-MM-DD"' },
                        { field: 'scoremonth', title: '月度评估分数', visible: false, operate: false },
                        {
                            field: 'levelmonth', title: '月度评估定级', searchList: ['A', 'B', 'C', 'D'], formatter: function (value, row, index) {
                                var rValue = "";
                                if (value == "A") {
                                    rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                                } else if (value == "B") {
                                    rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                                } else if (value == "C") {
                                    rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                                } else {
                                    rValue = '<a class="btn btn-xs btn-danger">' + value + '</a>';
                                }
                                return rValue;
                            }
                        },
                        
                        {
                            field: 'levelyear', title: '本年度月平均定级', searchList: ['A', 'B', 'C', 'D'], formatter: function (value, row, index) {
                                var rValue = "";
                                if (value == "A") {
                                    rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                                } else if (value == "B") {
                                    rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                                } else if (value == "C") {
                                    rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                                } else {
                                    rValue = '<a class="btn btn-xs btn-danger">' + value + '</a>';
                                }
                                return rValue;
                            }
                        },
                        //{ field: 'score', title: '上年度评估评分', visible: false, operate: false },
                        //{
                        //    field: 'level', title: '上年度评估定级', searchList: ['A', 'B', 'C', 'D'], formatter: function (value, row, index) {
                        //        var rValue = "";
                        //        if (value == "A") {
                        //            rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                        //        } else if (value == "B") {
                        //            rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                        //        } else if (value == "C") {
                        //            rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                        //        } else {
                        //            rValue = '<a class="btn btn-xs btn-danger">' + value + '</a>';
                        //        }
                        //        return rValue;
                        //    }
                        //},
                        {
                            field: 'is_audit', title: '是否审核', searchList: ['是', '否'], formatter: function (value, row, index) {
                                var rValue = "";
                                if (value == "是") {
                                    rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                                } else if (value == "否") {
                                    rValue = '<a class="btn btn-xs btn-danger">' + value + '</a>';
                                } else if (value == "外企") {
                                    rValue = '<a class="btn btn-xs btn-primary">' + value + '</a>';
                                } else {
                                    rValue = '<a class="btn btn-xs btn-success">' + value + '</a>';
                                }
                                return rValue;
                            }
                        },
						{
						    field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: function (value, row, index) {
						        var detail = '<a class="btn btn-xs btn-success btn-detail">详情</a>  ';
						        if (row.id == "4") {
						            detail = '<a class="btn btn-xs btn-success btn-detail">详情</a> <a class="btn btn-xs btn-success btn-audit">审核</a> ';
						        }
						        return detail + Table.api.formatter.operate.call(this, value, row, index, table);
						    }
						},
                        //自定义栏位
                        { field: 'custom', title: '评估',operate: false, events: Controller.api.events.operate, formatter: Controller.api.formatter.custom },
                    ],
                ],
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: true,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false,
                detailView: false,
                showExport: false
            });

            //在表格内容渲染完成后回调的事件
            table.on('post-body.bs.table', function (e, settings, json, xhr) {

            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //指定搜索条件
            $(document).on("click", ".btn-singlesearch", function () {
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    return {
                        search: params.search,
                        sort: params.sort,
                        order: params.order,
                        filter: JSON.stringify({ admin_id: 1 }),
                        op: JSON.stringify({ admin_id: '=' }),
                        offset: params.offset,
                        limit: params.limit,
                    };
                };
                table.bootstrapTable('refresh', {});
                Toastr.info("当前执行的是自定义搜索");
                return false;
            });

            //获取选中项
            $(document).on("click", ".btn-selected", function () {
                Layer.alert(JSON.stringify(table.bootstrapTable('getSelections')));
            });

            //启动和暂停按钮
            $(document).on("click", ".btn-start,.btn-pause", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                Table.api.multi("changestatus", 0, table, this);
            });

            //追加
            $(document).on("click", ".btn-append", function add_line() {
                var tbody = table.find('tbody');
                var tr = $('tr', tbody).eq(0).clone();
                tbody.append(tr);
            });

            //移除
            $(document).on("click", ".btn-remove", function add_line() {
                var tbody = table.find('tbody');
                var rows = tbody.find('tr').length - 1;
                if (rows > 0) {
                    table.find('tbody tr:last').remove();
                } else {
                    Toastr.info("至少保留一行");
                };
            });


            $('#jstree2').jstree({
                'core': {
                    'check_callback': true
                },
                'plugins': ['types', 'dnd'],
                'types': {
                    'default': {
                        'icon': 'fa fa-folder'
                    },
                    'html': {
                        'icon': 'fa fa-file-code-o'
                    },
                    'svg': {
                        'icon': 'fa fa-file-picture-o'
                    },
                    'css': {
                        'icon': 'fa fa-file-code-o'
                    },
                    'img': {
                        'icon': 'fa fa-file-image-o'
                    },
                    'son': {
                        'icon': 'fa fa-file-text-o'
                    }

                }
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                url: function (value, row, index) {
                    return '<div class="input-group input-group-sm" style="width:250px;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                },
                ip: function (value, row, index) {
                    return '<a class="btn btn-xs btn-ip bg-success"><i class="fa fa-map-marker"></i> ' + value + '</a>';
                },
                browser: function (value, row, index) {
                    //这里我们直接使用row的数据
                    return '<a class="btn btn-xs btn-browser">' + row.useragent.split(" ")[0] + '</a>';
                },
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var detail = '<a class="btn btn-xs btn-danger btn-think">服务类评估</a> ';
                    if (row.id == "3") {
                        detail = ' <a class="btn btn-xs btn-primary btn-appeal">申诉</a> ';
                    } else if (row.id == "4") {
                        detail = '';
                    }else {
                        //表示大类为多个
                        if (row.typeone == "物资类+工程服务类") {
                            detail = ' <a class="btn btn-xs btn-default btn-think">物资类评估</a><br/><a class="btn btn-xs btn-danger btn-thinks">服务类评估</a> ';
                        }
                        else if (row.typeone == "物资类评估") {
                            detail = ' <a class="btn btn-xs btn-default btn-think">物资类评估</a>';
                        } else {
                            detail = ' <a class="btn btn-xs btn-danger btn-thinks">服务类评估</a> ';
                        }
                    }
                    return detail;
                    //return '<a class="btn btn-xs btn-danger btn-change" data-url="example/bootstraptable/change" data-id="' + row.id + '">' + __('Locked') + '</a>';
                },
                operate: function (value, row, index) {
                    //返回字符串加上Table.api.formatter.operate的结果
                    //默认需要按需显示排序/编辑/删除按钮,则需要在Table.api.formatter.operate将table传入
                    //传入了table以后如果edit_url为空则不显示编辑按钮,如果del_url为空则不显显删除按钮
                    return '<a class="btn btn-info btn-xs btn-detail"><i class="fa fa-list"></i> ' + __('Detail') + '</a> '
                            + Table.api.formatter.operate(value, row, index, $("#table"));
                },
            },
            events: {//绑定事件的方法
                ip: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-ip': function (e, value, row, index) {
                        e.stopPropagation();
                        console.log();
                        var container = $("#table").data("bootstrap.table").$container;
                        var options = $("#table").bootstrapTable('getOptions');
                        //这里我们手动将数据填充到表单然后提交
                        $("form.form-commonsearch [name='ip']", container).val(value);
                        $("form.form-commonsearch", container).trigger('submit');
                        Toastr.info("执行了自定义搜索操作");
                    }
                },
                browser: {
                    'click .btn-browser': function (e, value, row, index) {
                        e.stopPropagation();
                        Layer.alert("该行数据为: <code>" + JSON.stringify(row) + "</code>");
                    }
                },
                operate: $.extend({
                    'click .btn-detail': function (e, value, row, index) {
                        e.stopPropagation();
                        Backend.api.open('supplier-detail.html', '供应商信息详情');
                    },
                    'click .btn-think': function (e, value, row, index) {
                        e.stopPropagation();
                        Backend.api.open('supplier-think.html', '供应商评估-物资类');
                    },
                    'click .btn-thinks': function (e, value, row, index) {
                        e.stopPropagation();
                        Backend.api.open('supplier-think.html', '供应商评估-工程服务类');
                    },
                    'click .btn-audit': function (e, value, row, index) {
                        e.stopPropagation();
                        Layer.alert('审核成功')
                    },
                    'click .btn-appeal': function (e, value, row, index) {
                        e.stopPropagation();
                        //Layer.alert("该行数据为: <code>" + JSON.stringify(row) + "</code>");
                        Layer.alert('申述成功')
                        //Backend.api.open('supplier-think.html', '供应商申诉');
                    }
                }, Table.api.events.operate)
            }
        }
    };
    return Controller;
});